package com.vision.util.http;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.util.StringUtils;

/**
 * @Filename AbsHttpFactory.java
 * @Description
 * @Version 1.0
 * @Author zhongc
 * @Email zhong_ch@foxmail.com
 * @History <li>Author: zhongc</li>
 * <li>Date: 2017/3/28</li>
 * <li>Version: 1.0</li>
 * <li>Content: create</li>
 */
public abstract class AbsHttpFactory {
    public static String defaultEncoding = "utf-8";
    public static final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36";

    private final HttpClientContext httpClientContext = HttpClientContext.create();
    HttpHost proxy;
    /**
     * socket Timeout
     */
    int socketTimeOut;

    /**
     * connect timeout
     */
    int connectTimeOut;

    /**
     * request timeout
     */
    int requestTimeOut;

    /**
     * cookie 类型  default CookieSpecs.default
     *
     * @see org.apache.http.client.config.CookieSpecs
     */
    String BEST_MATCH;

    public AbsHttpFactory(int socketTimeOut, int connectTimeOut, int requestTimeOut, String BEST_MATCH) {
        this.socketTimeOut = socketTimeOut;
        this.connectTimeOut = connectTimeOut;
        this.requestTimeOut = requestTimeOut;
        this.BEST_MATCH = BEST_MATCH;
    }

    public AbsHttpFactory(String proxyIp, int socketTimeOut, int connectTimeOut, int requestTimeOut, String BEST_MATCH) {
        this.setProxyWithOutPort(proxyIp);
        this.socketTimeOut = socketTimeOut;
        this.connectTimeOut = connectTimeOut;
        this.requestTimeOut = requestTimeOut;
        this.BEST_MATCH = BEST_MATCH;
    }


    public AbsHttpFactory(String proxyIp, Integer proxyPort, int socketTimeOut, int connectTimeOut, int requestTimeOut) {
        this.setProxy(proxyIp, proxyPort);
        this.socketTimeOut = socketTimeOut;
        this.connectTimeOut = connectTimeOut;
        this.requestTimeOut = requestTimeOut;
    }

    public AbsHttpFactory(String proxyIp, Integer proxyPort, int socketTimeOut, int connectTimeOut, int requestTimeOut, String BEST_MATCH) {
        this.setProxy(proxyIp, proxyPort);
        this.socketTimeOut = socketTimeOut;
        this.connectTimeOut = connectTimeOut;
        this.requestTimeOut = requestTimeOut;
        this.BEST_MATCH = BEST_MATCH;
    }

    private void setProxy(String proxyIp, Integer proxyPort) {
        if (!StringUtils.isEmpty(proxyIp) && proxyPort != null) {
            this.proxy = new HttpHost(proxyIp, proxyPort);
        }
    }

    private void setProxyWithOutPort(String proxyIp) {
        if (!StringUtils.isEmpty(proxyIp)) {
            this.proxy = new HttpHost(proxyIp);
        }
    }



    public abstract CloseableHttpClient getHttpClient();

    public abstract RequestConfig getRequestConfig();

    public HttpClientContext getHttpClientContext() {
        return httpClientContext;
    }
}
